* The world system and the hollowing-out of state capacity
* Replication file
***************************************************************

/* 	Table of contents:

  Data preparation
  Part 1: Main analysis
  Part 2: Appendices (main data)
  Part 3: BEEPS analysis
  Part 4: Diagnostics
*/

  use "IMFBQ_AJS_data", clear
  drop if incid==1

* *************************************************************
* Data preparation: RD-3
* *************************************************************

* Preliminaries  
  g lngdppc=ln(gdppc_WDI)
  g lnpop=ln(unna_pop)
  g lntrade=ln(trade_WDI)
  g oil_pc=100*ross_oil_prod/unna_pop
  g lnoilpc=ln(1+100*ross_oil_prod_QOG/unna_pop)
  g depratio=pop_0014+pop_65nn
  g lndepratio=ln(1+pop_0014+pop_65nn)
  
  foreach var in oda_pc fdi_gdp{
  g ln`var'=ln(1+`var')
  replace ln`var'=-ln(1-`var') if `var'<0
  }
  
  g compet_ex=dpi_eipc==7
  replace compet_ex=. if dpi_eipc==.
  g compet_leg=dpi_lipc==7
  replace compet_leg=. if dpi_lipc==.
  
  sort cid time
  xtset cid time
  
* IV variables  
  foreach var in SCB QCB cQCB imf1 BA2TOT BA2LAB BA2PRI BA2SOE{
	egen vmean=mean(`var'), by(cid)
	egen tmean=mean(`var'), by(time)
	g iv`var'=nUnder*vmean
	g iv2`var'=tmean*vmean
	drop vmean tmean
  }
  
  	
* *************************************************************
* Part 1: Main analysis
* *************************************************************
 
  global BMA1  lngdppc p_polity2_QOG civwar_UCDP gdp_growth_WDI
  global BMA2  lngdppc p_polity2_QOG civwar_UCDP gdp_growth_WDI lndepratio lntrade lnfdi_gdp lnoda_pc lnoilpc  

  global Z1  limf1 nUnder s_unga3g7
  global Z2  $Z1 reserves_WDI debtser_gni cab_gdp dpi_exelec dpi_legelec
    
  
* Table 1: Total effect
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  est store a1
  drop imr
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  est store a2
  drop imr 
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  est store a3
  drop imr 
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  est store a4
  drop imr 
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  est store a5
  drop imr 
    
  estout a*, starlevels(* .1 ** .05 *** .01) cells(b(star fmt(2)) se(par fmt(2))) stats(N chi2, fmt(%7.0f %7.2f)) keep(icrg*:) drop(*cid* *time* _cons) order(imf1)
  

* Table 2: IMF conditionality 
  cmp setup 
  set matsize 800 
  qui xi:reg icrgBQ100 i.cid i.time  
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=SCB QCB imr l.icrgBQ100 _Icid* _Itime*) (SCB=ivSCB _Icid* _Itime*) (QCB=ivQCB _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid)
  est store e1
  test ([SCB]ivSCB [QCB]ivQCB)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=SCB QCB l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (SCB=ivSCB $BMA1 _Icid* _Itime*) (QCB=ivQCB $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid)
  est store e2
  test ([SCB]ivSCB [QCB]ivQCB)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=SCB QCB l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (SCB=ivSCB $BMA2 _Icid* _Itime*) (QCB=ivQCB $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid)
  est store e3
  test ([SCB]ivSCB [QCB]ivQCB)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=SCB QCB l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (SCB=ivSCB $BMA1 _Icid* _Itime*) (QCB=ivQCB $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid)
  est store e4
  test ([SCB]ivSCB [QCB]ivQCB)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=SCB QCB l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (SCB=ivSCB $BMA2 _Icid* _Itime*) (QCB=iv2QCB $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid)
  est store e5
  test ([SCB]ivSCB [QCB]iv2QCB)
  drop imr
  
  estout e*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(2)) se(par fmt(2))) stats(N1 ll, fmt(%7.0f %7.2f)) keep(icrgBQ100:) drop(*cid* *time* _cons)
  

* Table 3: Further mechanisms
  ** BA2TOT
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2TOT imr l.icrgBQ100 _Icid* _Itime*) (BA2TOT=ivBA2TOT _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store f1
  test ([BA2TOT]ivBA2TOT)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2TOT l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (BA2TOT=ivBA2TOT $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store f2
  test ([BA2TOT]ivBA2TOT)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2TOT l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (BA2TOT=ivBA2TOT $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store f3
  test ([BA2TOT]ivBA2TOT)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2TOT l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (BA2TOT=ivBA2TOT $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store f4
  test ([BA2TOT]ivBA2TOT)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2TOT l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (BA2TOT=ivBA2TOT $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store f5
  test ([BA2TOT]ivBA2TOT)
  drop imr
  
  ** BA2LAB
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2LAB imr l.icrgBQ100 _Icid* _Itime*) (BA2LAB=ivBA2LAB _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store g1
  test ([BA2LAB]ivBA2LAB)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2LAB l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (BA2LAB=ivBA2LAB $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store g2
  test ([BA2LAB]ivBA2LAB)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2LAB l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (BA2LAB=ivBA2LAB $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store g3
  test ([BA2LAB]ivBA2LAB)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2LAB l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (BA2LAB=ivBA2LAB $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store g4
  test ([BA2LAB]ivBA2LAB)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2LAB l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (BA2LAB=ivBA2LAB $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store g5
  test ([BA2LAB]ivBA2LAB)
  drop imr
  
  ** BA2PRI
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2PRI imr l.icrgBQ100 _Icid* _Itime*) (BA2PRI=ivBA2PRI _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store h1
  test ([BA2PRI]ivBA2PRI)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2PRI l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (BA2PRI=ivBA2PRI $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store h2
  test ([BA2PRI]ivBA2PRI)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2PRI l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (BA2PRI=ivBA2PRI $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store h3
  test ([BA2PRI]ivBA2PRI)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2PRI l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (BA2PRI=ivBA2PRI $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store h4
  test ([BA2PRI]ivBA2PRI)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2PRI l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (BA2PRI=ivBA2PRI $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store h5
  test ([BA2PRI]ivBA2PRI)
  drop imr
  
  ** BA2SOE
   qui treatreg icrgBQ100 imf1 l.icrgBQ100 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2SOE imr l.icrgBQ100 _Icid* _Itime*) (BA2SOE=ivBA2SOE _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store i1
  test ([BA2SOE]ivBA2SOE)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2SOE l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (BA2SOE=ivBA2SOE $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store i2
  test ([BA2SOE]ivBA2SOE)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2SOE l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (BA2SOE=ivBA2SOE $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store i3
  test ([BA2SOE]ivBA2SOE)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2SOE l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (BA2SOE=ivBA2SOE $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store i4
  test ([BA2SOE]ivBA2SOE)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=BA2SOE l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (BA2SOE=ivBA2SOE $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store i5
  test ([BA2SOE]ivBA2SOE)
  drop imr
    
  estout f*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(2)) se(par fmt(2))) stats(N1 ll, fmt(%7.0f %7.2f)) keep(icrgBQ100:BA*)
  estout g*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(2)) se(par fmt(2))) stats(N1 ll, fmt(%7.0f %7.2f)) keep(icrgBQ100:BA*)
  estout h*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(2)) se(par fmt(2))) stats(N1 ll, fmt(%7.0f %7.2f)) keep(icrgBQ100:BA*)
  estout i*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(2)) se(par fmt(2))) stats(N1 ll, fmt(%7.0f %7.2f)) keep(icrgBQ100:BA*)
  

* *************************************************************
* Part 2: Appendices
* *************************************************************

* Appendix A
* **********

  preserve

   keep if time==6
   ** 1996-98 **

   sort iso3 
   merge iso3 using "qog_exp_agg_15"
   tab _merge
   drop if _merge==2
   drop _merge
  
   sort iso3 
   merge iso3 using "ER1999"
   tab _merge
   drop if _merge==2
   drop _merge
   
   sort iso3 
   merge iso3 using "IMFBQ-BEEPS"
   tab _merge 
   drop if _merge==2
   drop _merge

   corr icrgBQ ge_WGI 
   corr icrgBQ webscale merit career salary burqual 
   corr icrgBQ proff impar closed 
   corr icrgBQ br_inc 
   corr icrgBQ gdp_growth_WDI

  restore


* Appendix B
* **********  

* Table B1 - Descriptives  
  global Z nUnder s_unga3g7 reserves_WDI debtser_gni cab_gdp dpi_exelec dpi_legelec
  estpost summarize icrgBQ100 imf1 SCB QCB BA2TOT BA2LAB BA2PRI BA2SOE gdppc_WDI p_polity2_QOG civwar_UCDP gdp_growth_WDI depratio trade fdi_gdp oda_pc oil_pc $Z if icrgBQ100!=.
  esttab, cells("count mean sd min max") noobs
  
* Correlation table (Appendix D)
  corr icrgBQ100 imf1 SCB QCB BA2TOT BA2LAB BA2PRI BA2SOE gdppc_WDI p_polity2_QOG civwar_UCDP gdp_growth_WDI depratio trade fdi_gdp oda_pc oil_pc $Z if icrgBQ100!=.

    
* Table B3
  ** cSCB
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cSCB imr l.icrgBQ100 _Icid* _Itime*) (cSCB=ivcSCB _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store c1
  test ([cSCB]ivcSCB)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cSCB l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (cSCB=ivcSCB $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store c2
  test ([cSCB]ivcSCB)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z1 $BMA2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cSCB l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (cSCB=ivcSCB $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store c3
  test ([cSCB]ivcSCB)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cSCB l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (cSCB=ivcSCB $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store c4
  test ([cSCB]ivcSCB)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cSCB l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (cSCB=ivcSCB $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store c5
  test ([cSCB]ivcSCB)
  drop imr
  
  esttab c*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 ll, fmt(%7.0f %7.2f)) keep(icrgBQ100:) drop(*cid* *time* _cons)
  esttab c*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 ll, fmt(%7.0f %7.2f)) keep(cSCB:) drop(*cid* *time* _cons)
    
  ** cBALAB
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cBALAB imr l.icrgBQ100 _Icid* _Itime*) (cBALAB=ivcBALAB _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store f1
  test ([cBALAB]ivcBALAB)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cBALAB l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (cBALAB=ivcBALAB $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store f2
  test ([cBALAB]ivcBALAB)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cBALAB l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (cBALAB=ivcBALAB $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store f3
  test ([cBALAB]ivcBALAB)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cBALAB l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (cBALAB=ivcBALAB $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store f4
  test ([cBALAB]ivcBALAB)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cBALAB l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (cBALAB=ivcBALAB $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store f5
  test ([cBALAB]ivcBALAB)
  drop imr
  
  estout f*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 ll, fmt(%7.0f %7.2f)) keep(icrgBQ100:) drop(*cid* *time* _cons)
  estout f*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 ll, fmt(%7.0f %7.2f)) keep(cBALAB:) drop(*cid* *time* _cons)  
    
  ** cBAPRI
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cBAPRI imr l.icrgBQ100 _Icid* _Itime*) (cBAPRI=ivcBAPRI _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store g1
  test ([cBAPRI]ivcBAPRI)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cBAPRI l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (cBAPRI=ivcBAPRI $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store g2
  test ([cBAPRI]ivcBAPRI)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cBAPRI l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (cBAPRI=ivcBAPRI $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store g3
  test ([cBAPRI]ivcBAPRI)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cBAPRI l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (cBAPRI=ivcBAPRI $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store g4
  test ([cBAPRI]ivcBAPRI)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cBAPRI l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (cBAPRI=ivcBAPRI $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store g5
  test ([cBAPRI]ivcBAPRI)
  drop imr
  
  estout g*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 ll, fmt(%7.0f %7.2f)) keep(icrgBQ100:) drop(*cid* *time* _cons)
  estout g*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 ll, fmt(%7.0f %7.2f)) keep(cBAPRI:) drop(*cid* *time* _cons)  
    
  ** cBASOE
   qui treatreg icrgBQ100 imf1 l.icrgBQ100 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cBASOE imr l.icrgBQ100 _Icid* _Itime*) (cBASOE=ivcBASOE _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store h1
  test ([cBASOE]ivcBASOE)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cBASOE l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (cBASOE=ivcBASOE $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store h2
  test ([cBASOE]ivcBASOE)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cBASOE l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (cBASOE=ivcBASOE $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store h3
  test ([cBASOE]ivcBASOE)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cBASOE l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (cBASOE=ivcBASOE $BMA1 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store h4
  test ([cBASOE]ivcBASOE)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=cBASOE l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (cBASOE=ivcBASOE $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont) cl(cid)
  est store h5
  test ([cBASOE]ivcBASOE)
  drop imr
    
  estout h*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 ll, fmt(%7.0f %7.2f)) keep(icrgBQ100:) drop(*cid* *time* _cons)
  estout h*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 ll, fmt(%7.0f %7.2f)) keep(cBASOE:) drop(*cid* *time* _cons)  

  
* Table B4

  sort iso3 time
  merge iso3 using "FCS countries"
  drop if _merge==2
  drop _merge
  sort cid time

  ** Fragile states (fcs_wb)
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 i.cid i.time, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=SCB QCB imr l.icrgBQ100 _Icid* _Itime*) (SCB=ivSCB _Icid* _Itime*) (QCB=ivQCB _Icid* _Itime*) if imf1==1 & !fcs, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid) difficult
  est store ef1
  test ([SCB]ivSCB [QCB]ivQCB)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time if !fcs, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=SCB QCB l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (SCB=ivSCB $BMA1 _Icid* _Itime*) (QCB=ivQCB $BMA1 _Icid* _Itime*) if imf1==1 & !fcs, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid) difficult
  est store ef2
  test ([SCB]ivSCB [QCB]ivQCB)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=SCB QCB l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (SCB=ivSCB $BMA1 _Icid* _Itime*) (QCB=ivQCB $BMA1 _Icid* _Itime*) if imf1==1 & !fcs, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid) difficult
  est store ef3
  test ([SCB]ivSCB [QCB]ivQCB)
  drop imr

  estout ef*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 ll, fmt(%7.0f %7.2f)) keep(icrgBQ100:) drop(*cid* *time* _cons)
  estout ef*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 ll, fmt(%7.0f %7.2f)) keep(QCB:) drop(*cid* *time* _cons)  

  ** Low-income countries (incid==2)
  g lic=incid==2
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 i.cid i.time if !lic, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=SCB QCB imr l.icrgBQ100 _Icid* _Itime*) (SCB=ivSCB _Icid* _Itime*) (QCB=ivQCB _Icid* _Itime*) if imf1==1 & !lic, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid) 
  est store ef4
  test ([SCB]ivSCB [QCB]ivQCB)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time if !lic, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=SCB QCB l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (SCB=ivSCB $BMA1 _Icid* _Itime*) (QCB=ivQCB $BMA1 _Icid* _Itime*) if imf1==1 & !lic, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid) difficult
  est store ef5
  test ([SCB]ivSCB [QCB]ivQCB)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time if !lic, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=SCB QCB l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (SCB=ivSCB $BMA1 _Icid* _Itime*) (QCB=ivQCB $BMA1 _Icid* _Itime*) if imf1==1 & !lic, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid) difficult
  est store ef6
  test ([SCB]ivSCB [QCB]ivQCB)
  drop imr


  ** Lowest 10% of BQ in 1981-1986
  egen meanBQ=mean(icrgBQ) if time>0 & time<=2, by(cid)
  su meanBQ, d
  g lowBQ=(meanBQ<`r(p10)')

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 i.cid i.time if !lowBQ, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=SCB QCB imr l.icrgBQ100 _Icid* _Itime*) (SCB=ivSCB _Icid* _Itime*) (QCB=ivQCB _Icid* _Itime*) if imf1==1 & !lowBQ, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid) 
  est store ef7
  test ([SCB]ivSCB [QCB]ivQCB)
  drop imr
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time if !lowBQ, treat(imf1=$Z1 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=SCB QCB l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (SCB=ivSCB $BMA1 _Icid* _Itime*) (QCB=ivQCB $BMA1 _Icid* _Itime*) if imf1==1 & !lowBQ, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid) difficult
  est store ef8
  test ([SCB]ivSCB [QCB]ivQCB)
  drop imr

  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA1 i.cid i.time if !lowBQ, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=SCB QCB l.icrgBQ100 $BMA1 imr _Icid* _Itime*) (SCB=ivSCB $BMA1 _Icid* _Itime*) (QCB=ivQCB $BMA1 _Icid* _Itime*) if imf1==1 & !lowBQ, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid) difficult
  est store ef9
  test ([SCB]ivSCB [QCB]ivQCB)
  drop imr

  estout ef*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 ll, fmt(%7.0f %7.2f)) keep(icrgBQ100:) drop(*cid* *time* _cons)
  estout ef*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 ll, fmt(%7.0f %7.2f)) keep(QCB:) drop(*cid* *time* _cons)  


* Table B5 - selection model
  estout a*, starlevels(* .1 ** .05 *** .01) cells(b(star fmt(2)) se(par fmt(2))) stats(N chi2, fmt(%7.0f %7.2f)) keep(imf1:) drop(*regid* *time* _cons)

  
* Table B6 - conditionality equation 
  estout e*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(2)) se(par fmt(2))) stats(N1 ll, fmt(%7.0f %7.2f)) keep(SCB:) drop(*cid* *time* _cons)  

  
* Additional sample issues 

  ** Sample statistics 
  bys imf1: su $BMA1
  tab cid
  tab time
  tab cid if icrgBQ100!=.
  tab time if icrgBQ100!=.

  ** Missingness patterns
  g miss=(icrgBQ100!=.)  
  xtreg miss $BMA1 imf1 i.time, fe cl(cid)
  est store m1
  xtreg miss $BMA1 imf1 i.time, re cl(cid)
  est store m2

  estout m*, starlevels(* .1  ** .05  *** .01) cells(b(star fmt(3)) se(par fmt(3))) stats(N1 ll, fmt(%7.0f %7.2f)) keep() drop( *time* _cons)

  

* *************************************************************
* Part 3: BEEPS analysis
* *************************************************************

preserve
  
* Data   
  rename time timeOLD
  gen timeID=timeOLD+1
  lab var timeID "One-period lagged variables from main data"
  tab timeID
  drop if timeID<=7
  drop if incid==1
  sort iso3 timeID
  merge iso3 timeID using "BEEPS_20160728.dta"
  tab _merge
  sort cid timeID
  xtset cid timeID

  order cid iso3 timeID 
  drop lngdppc lnpop lnoilpc
  g lngdppc=ln(gdppc_WDI)
  g lnpop=ln(unna_pop)
  g lnoilpc=ln(1+(1E+6)*ross_oil_prod/unna_pop)
  rename transparency HRV_index
  replace dy_colGBR=ceil(dy_colGBR)
  
  gen NonMiss=(br_inc!=.)
  egen N=sum(NonMiss), by(cid)
  drop NonMiss 
  
  
* Table B2 - Descriptive statistics 
  estpost summarize br_inc imf1 BA2TOT SCB QCB lngdppc p_polity2_QOG p_durable_QOG lnoilpc min_gdp_WDI  age dom_gov for_prv inp_foreign0 inp_foreign  days_exp days_imp comp_pct  if N>1 
  esttab, cells("count mean sd min max") noobs replace label   
  
  corr br_inc imf1 BA2TOT SCB QCB lngdppc p_polity2_QOG p_durable_QOG lnoilpc min_gdp_WDI  age dom_gov for_prv inp_foreign0 inp_foreign  days_exp days_imp comp_pct  if N>1 
  
  
* Table 4
  
  global X lngdppc p_polity2_QOG p_durable_QOG lnoilpc min_gdp_WDI
  global Firm age dom_gov for_prv inp_foreign0 inp_foreign
  global FirmEnd days_exp days_imp comp_pct 
  qui xi:reg br_inc i.cid i.timeID
  
  qui treatreg br_inc imf1 $X i.cid i.timeID if N>1, treat(imf1=limf1 nUnder s_unga3g7 gdp_growth_lag $X i.regid i.timeID) twostep hazard(imr)
  qui xtreg br_inc imf1 imr $X i.timeID if N>1, fe cl(cid)   
  est store bee21x
  drop imr
  
  qui treatreg br_inc SCB QCB imf1 $X i.cid i.timeID if N>1, treat(imf1=limf1 nUnder s_unga3g7 gdp_growth_lag $X i.regid i.timeID) twostep hazard(imr)
  qui cmp (br_inc= SCB QCB imf1 imr $X _Icid* _ItimeID*) (SCB=lSCB $X _Itime*) (QCB=lQCB $X _Itime*) if N>1, indicators($cmp_cont $cmp_cont $cmp_cont) ro cl(cid)
  est store bee31x
  test ([SCB]lSCB [QCB]lQCB)
  drop imr
  
  qui treatreg br_inc imf1 $X $Firm i.cid i.timeID if N>1, treat(imf1=limf1 nUnder s_unga3g7 gdp_growth_lag $X $Firm i.regid i.timeID) twostep hazard(imr)
  qui xtreg br_inc imf1 imr $X $Firm i.timeID if N>1, fe cl(cid)   
  est store bee21f
  drop imr
  
  qui treatreg br_inc SCB QCB imf1 $X $Firm i.cid i.timeID if N>1, treat(imf1=limf1 nUnder s_unga3g7 gdp_growth_lag $X $Firm i.regid i.timeID) twostep hazard(imr)
  qui cmp (br_inc= SCB QCB imf1 imr $X $Firm _Icid* _ItimeID*) (SCB=lSCB $X $Firm _Itime*) (QCB=lQCB $X $Firm _Itime*) if N>1, indicators($cmp_cont $cmp_cont $cmp_cont) ro cl(cid)
  est store bee31f
  test ([SCB]lSCB [QCB]lQCB)
  drop imr
  
estout bee2*x bee3*x bee2*f bee3*f, starlevels(* .1 ** .05 *** .01) cells(b(star fmt(2)) se(par fmt(2))) stats(r2_w N, fmt(%7.2f %7.0f)) drop(_cons *time*) keep(imf1 *SC* *QC* imr _cons *time*) order(imf1 *SC* *QC* imr) noabbrev

restore  
  
  
* *************************************************************
* Part 4: Diagnostics 
* *************************************************************

* Unit root test
 
  xtunitroot ips icrgBQ100, lag(aic 2)
  xtunitroot ips icrgBQ100, lag(aic 2) trend
  xtunitroot ips icrgBQ100, lag(aic 2) trend demean

  xtunitroot fisher icrgBQ100, l(1) demean dfuller
  xtunitroot fisher icrgBQ100, l(2) demean dfuller
  
  xtunitroot fisher icrgBQ100, l(1) demean pperron
  xtunitroot fisher icrgBQ100, l(2) demean pperron
  
  
* Wooldridge test for serial correlation
  
  qui xi:reg icrgBQ100 i.cid i.time
  qui treatreg icrgBQ100 imf1 $BMA2 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=SCB QCB $BMA2 imr _Icid* _Itime*) (SCB=ivSCB $BMA2 _Icid* _Itime*) (QCB=iv2QCB $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid) difficult
  
  qui predict yhat, xb
  qui g res=icrgBQ100-yhat
  reg res l.res, cl(cid)
  test (l.res)
  drop imr yhat 
  
  qui treatreg icrgBQ100 imf1 l.icrgBQ100 $BMA2 i.cid i.time, treat(imf1=$Z2 i.regid i.time) twostep hazard(imr)
  qui cmp (icrgBQ100=SCB QCB l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (SCB=ivSCB $BMA2 _Icid* _Itime*) (QCB=iv2QCB $BMA2 _Icid* _Itime*) if imf1==1, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid) difficult

  qui predict yhat, xb
  qui g res=icrgBQ100-yhat
  twoway scatter res l.res, mlabel(iso3)
  g outlier=(res<-25 & l.res<-25 | res>20 & l.res>20)
  reg res l.res, cl(cid)
  test (l.res)
  
  qui cmp (icrgBQ100=SCB QCB l.icrgBQ100 $BMA2 imr _Icid* _Itime*) (SCB=ivSCB $BMA2 _Icid* _Itime*) (QCB=iv2QCB $BMA2 _Icid* _Itime*) if imf1==1 & !outlier, indicators($cmp_cont $cmp_cont $cmp_cont) cl(cid) difficult
  qui predict yhat2, xb
  qui g res2=icrgBQ100-yhat2
  reg res2 l.res2, cl(cid)
  test (l.res2)
  
  drop imr yhat* res res2
